#include "Physics.hpp"

namespace zen {

	namespace physics {


		DebugDrawer*					Physics::defaultDebugDrawer;
		std::vector<PPhysicsContext>	Physics::contexts;


		DebugDrawer* Physics::getDefaultDebugDrawer() {
			if (defaultDebugDrawer == NULL) {
				defaultDebugDrawer = new DebugDrawer();
				defaultDebugDrawer->setDebugMode(btIDebugDraw::DBG_DrawWireframe);
			}
			return defaultDebugDrawer;
		}

		PPhysicsContext Physics::createPhysicsContext() {
			PhysicsContext* context = new PhysicsContext();
			PPhysicsContext pcontext(context);
			context->dynamicsWorld->setDebugDrawer(getDefaultDebugDrawer());
			for (unsigned int i = 0; i < contexts.size(); i++) {
				if (contexts[i] == NULL) {
					context->id = i;
					contexts[i] = pcontext;
					return pcontext;
				}
			}
			context->id = (int)contexts.size();
			contexts.push_back(pcontext);
			return pcontext;
		}

		void Physics::releasePhysicsContext(PPhysicsContext physicsContext) {
			if (physicsContext->id >= 0) {
				contexts[physicsContext->id] = PPhysicsContext();
			}
		}


	} // namespace physics

} // namespace zen
